//
//  HWSinglePersonViewController.m
//  HuaWan
//
//  Created by HuaWan on 2023/10/23.
//  Copyright © 2023 HuaWan. All rights reserved.
//

#import "HWSinglePersonViewController.h"

#import "HWSinglePersonImageCell.h"
#import "HWDoubleButtonCell.h"
#import "HWSingleButtonCell.h"
#import "HWDoubleImageCell.h"

#import "HWSinglePersonViewController.h"

#import "HWSinglePersonAlertView.h"

@interface HWSinglePersonViewController ()

@property (nonatomic,copy) NSString *big;
@property (nonatomic,copy) NSString *left;
@property (nonatomic,copy) NSString *right;
@property (nonatomic, strong) HWSinglePersonAlertView *singlePersonAlertView;

@end

@implementation HWSinglePersonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib
    self.view.backgroundColor = [UIColor blackColor];
    [self setNavigationItemTitleViewWithBlackTitle:@"石头剪刀布"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"HWSinglePersonImageCell" bundle:nil] forCellReuseIdentifier:@"HWSinglePersonImageCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HWDoubleButtonCell" bundle:nil] forCellReuseIdentifier:@"HWDoubleButtonCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HWSingleButtonCell" bundle:nil] forCellReuseIdentifier:@"HWSingleButtonCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HWDoubleImageCell" bundle:nil] forCellReuseIdentifier:@"HWDoubleImageCell"];
    
    self.tableView.frame = CGRectMake(0,self.navigationBarHeight, kScreenWidth,kScreenHeight - self.navigationBarHeight);
}

- (void)showBack {
    NSString *imageName = @"white_back";
    [self setLeftItemWithIcon:[UIImage imageNamed:imageName] selector:@selector(backAction:)];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

#pragma mark - cellDataSource

- (NSArray *)cellDataSource {
    
    if (!self.dataSource) {
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:4];
        
        __block NSMutableArray *subarr = nil;
        __block NSDictionary *dict = nil;
             
        subarr = [NSMutableArray arrayWithCapacity:1];
        
        if ([_type isEqualToString:@"双人"]) {
            //双人
            dict =  @{@"class":HWDoubleImageCell.class,
                      @"height":@([HWDoubleImageCell getCellFrame:[NSNumber numberWithFloat:400]]),
                      @"data":[HWDoubleImageCellModel ittemModelWithLeftTitle:_left rightTitle:_right]};
            [subarr addObject:dict];
            
            dict =  @{@"class":HWDoubleButtonCell.class,
                      @"height":@([HWDoubleButtonCell getCellFrame:[NSNumber numberWithFloat:75]]),
                      @"data":[HWDoubleButtonCellModel ittemModelWithLeftTitle:@"左" rightTitle:@"右"]};
            [subarr addObject:dict];
            
            dict =  @{@"class":HWSingleButtonCell.class,
                      @"height":@([HWSingleButtonCell getCellFrame:[NSNumber numberWithFloat:235]]),
                      @"data":[HWSingleButtonCellModel ittemModelWithTitle:@"开始"],
                      @"delegate":@YES};
            [subarr addObject:dict];
            
        } else {
            //单人
            dict =  @{@"class":HWSinglePersonImageCell.class,
                      @"height":@([HWSinglePersonImageCell getCellFrame:[NSNumber numberWithFloat:kScreenWidth]]),
                      @"data":[HWSinglePersonImageCellModel ittemModelWithResult:_big]};
            [subarr addObject:dict];
            
            dict =  @{@"class":HWDoubleButtonCell.class,
                      @"height":@([HWDoubleButtonCell getCellFrame:[NSNumber numberWithFloat:75]]),
                      @"data":[HWDoubleButtonCellModel ittemModelWithLeftTitle:@"随机" rightTitle:@"选择"],
                      @"delegate":@YES};
            [subarr addObject:dict];
        }

        [arr addObject:subarr];

        self.dataSource = arr;
    }
    return self.dataSource;
}

#pragma mark HWDoubleButtonCellDelegate 随机 选择

- (void)doubleButtonCell:(HWDoubleButtonCell *)cell button:(UIButton *)sender {
    if ([_type isEqualToString:@"单人"]) {
        if (sender.tag == 10) {
            //随机
            [self randomButtonAction:sender];
        } else if (sender.tag == 11) {
            //选择
            [self.singlePersonAlertView show];
        }
    }
}

#pragma mark HWSingleButtonCellDelegate 开始

- (void)singleButtonCell:(HWSingleButtonCell *)cell button:(UIButton *)sender {
    [self randomButtonAction:sender];
}

- (void)randomButtonAction:(UIButton *)sender {
    if ([_type isEqualToString:@"双人"]) {
        NSInteger i = arc4random() % 3;
        _left = self.resultAry[i];
        i = arc4random() % 3;
        _right = self.resultAry[i];
    } else {
        NSInteger i = arc4random() % 3;
        _big = self.resultAry[i];
    }
    [self refreshData];
}

- (NSArray *)resultAry {
    NSArray *ary = @[@"石头",@"剪刀",@"布"];
    return ary;
}

#pragma mark - 懒加载

- (HWSinglePersonAlertView *)singlePersonAlertView {
    if (!_singlePersonAlertView) {
        _singlePersonAlertView  = [HWSinglePersonAlertView instanceView];
        _singlePersonAlertView.frame = CGRectMake(0,0, kScreenWidth, kScreenHeight);
        @weakify(self);
        _singlePersonAlertView.buttonAction = ^(HWSinglePersonAlertView *alertView, UIButton *sender) {
            @strongify(self);
            if (sender.tag == 10) {
                self->_big = @"石头";
            } else if (sender.tag == 11) {
                self->_big = @"剪刀";
            } else if (sender.tag == 12) {
                self->_big = @"布";
            }
            [self refreshData];
        };
        //获取当前UIWindow 并添加一个视图
        UIApplication *ap = [UIApplication sharedApplication];
        [ap.keyWindow addSubview:_singlePersonAlertView];
    }
    return _singlePersonAlertView;
}

@end
